/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package camada.apresentacao.dialog.servico;

import camada.bancodedados.daoimplemente.servico.ServicoDAOImpl;
import eloma.classe.servico.Servico;
import java.awt.Color;
import java.awt.event.KeyEvent;
import java.util.List;
import javax.swing.ListSelectionModel;
import tabelamodelcolumnmodel.servico.ServicoColumnModel;
import tabelamodelcolumnmodel.servico.ServicoTableModel;

/**
 *
 * @author GEMADA
 */
public class DialogServico extends javax.swing.JDialog {

    int indexTabela, idEmpresa;
    boolean pesquisa;
    List<Servico> listaServico;
    ServicoDAOImpl daoServico = new ServicoDAOImpl();

    /**
     * Creates new form DialogServico
     */
    public DialogServico(java.awt.Frame parent, boolean modal, int idEmpresa) {
        super(parent, modal);
        initComponents();
        this.idEmpresa = idEmpresa;
        this.setLocationRelativeTo(null);
        this.setTitle("Serviços");
        configurarTabela(null);
    }

    public void configurarTabela(List<Servico> lista) {
        try {

            if (lista == null) {
                listaServico = daoServico.selecionarServico();
            } else {
                listaServico = lista;
            }

            //this.setListaCliente(listaCliente);
            tabelaServico.setModel(new ServicoTableModel(listaServico));

            tabelaServico.setAutoCreateColumnsFromModel(false);
            java.awt.FontMetrics fm = tabelaServico.getFontMetrics(tabelaServico.getFont());

            //Aki tem um exemplo de como colocar a MyCenderSelll.
            tabelaServico.setColumnModel(new ServicoColumnModel(fm));
            tabelaServico.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        } catch (Exception e) {
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btVoltar = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabelaServico = new javax.swing.JTable();
        btPesquisa = new javax.swing.JButton();
        btEditar = new javax.swing.JButton();
        btRemover = new javax.swing.JButton();
        btCancelar = new javax.swing.JButton();
        txPesquisa = new javax.swing.JTextField();
        btAdicionar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        btVoltar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ima/cadastro/Voltar.png"))); // NOI18N
        btVoltar.setText("Voltar");
        btVoltar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btVoltarActionPerformed(evt);
            }
        });

        tabelaServico.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        tabelaServico.setToolTipText("Tabela de Clientes Cadastrados");
        tabelaServico.setSelectionBackground(new java.awt.Color(255, 255, 0));
        tabelaServico.setSelectionForeground(new java.awt.Color(0, 0, 0));
        tabelaServico.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tabelaServicoMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tabelaServico);

        btPesquisa.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ima/outros/Pesquisa.png"))); // NOI18N
        btPesquisa.setText("Pesquisar");
        btPesquisa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btPesquisaActionPerformed(evt);
            }
        });

        btEditar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ima/cadastro/Editar.png"))); // NOI18N
        btEditar.setText("Editar");
        btEditar.setEnabled(false);
        btEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btEditarActionPerformed(evt);
            }
        });

        btRemover.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ima/cadastro/Remove.png"))); // NOI18N
        btRemover.setText("Remover");
        btRemover.setEnabled(false);
        btRemover.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btRemoverActionPerformed(evt);
            }
        });

        btCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ima/cadastro/Cancel.png"))); // NOI18N
        btCancelar.setText("Cancelar");
        btCancelar.setEnabled(false);
        btCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCancelarActionPerformed(evt);
            }
        });

        txPesquisa.setForeground(new java.awt.Color(153, 153, 153));
        txPesquisa.setText("Digite um nome para ser pesquisado");
        txPesquisa.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txPesquisaMouseClicked(evt);
            }
        });
        txPesquisa.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txPesquisaFocusLost(evt);
            }
        });
        txPesquisa.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txPesquisaKeyPressed(evt);
            }
        });

        btAdicionar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ima/cadastro/Add.png"))); // NOI18N
        btAdicionar.setText("Adicionar");
        btAdicionar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAdicionarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btAdicionar, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btEditar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btRemover)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btCancelar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btVoltar))
                    .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 689, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(txPesquisa, javax.swing.GroupLayout.PREFERRED_SIZE, 329, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btPesquisa))
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 679, Short.MAX_VALUE))
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btAdicionar)
                    .addComponent(btEditar)
                    .addComponent(btRemover)
                    .addComponent(btCancelar)
                    .addComponent(btVoltar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txPesquisa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btPesquisa))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 262, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btVoltarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btVoltarActionPerformed
        this.hide();
    }//GEN-LAST:event_btVoltarActionPerformed

    private void tabelaServicoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tabelaServicoMouseClicked
        try {

            indexTabela = tabelaServico.getSelectedRow();

            if (indexTabela > -1) {

                btAdicionar.setEnabled(false);
                btVoltar.setEnabled(false);
                btEditar.setEnabled(true);
                btRemover.setEnabled(true);
                btCancelar.setEnabled(true);
            }

        } catch (Exception e) {
        }
    }//GEN-LAST:event_tabelaServicoMouseClicked

    private void btPesquisaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btPesquisaActionPerformed
        try {

            //            List<Cliente> listaAux = daoCliente.pesquisaClientePorNome(txPesquisa.getText());
            //
            //            if (listaCliente.size() > 0) {
            //
            //                for (int j = 0; j < listaCliente.size(); j++) {
            //
            //                    Cliente c = (Cliente) listaCliente.get(j);
            //
            //                    if (c.getNome().equals(txPesquisa.getText())) {
            //                        listaAux.add(c);
            //                    }
            //                }
            //
            //                configurarTabela(listaAux);
            //
            //            }else{
            //
            //                List<Cliente> lista = daoCliente.pesquisarTodosClientes();
            //
            //                configurarTabela(lista);
            //            }
        } catch (Exception e) {

        }
    }//GEN-LAST:event_btPesquisaActionPerformed

    private void btEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btEditarActionPerformed
        try {

            ServicoTableModel model = (ServicoTableModel) tabelaServico.getModel();
            Servico servico = model.getValoresServico(indexTabela);

            DialogCadServico cadServico = new DialogCadServico(null, true, this.idEmpresa);
            cadServico.DialogCadServico("Editar");
            cadServico.show();

        } catch (Exception e) {
        }
    }//GEN-LAST:event_btEditarActionPerformed

    private void btRemoverActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btRemoverActionPerformed
        try {

            ServicoTableModel model = (ServicoTableModel) tabelaServico.getModel();
            Servico servico = model.getValoresServico(indexTabela);

            //daoServio.removerServico(servico.getIdServico());
            //configurarTabela();
            btAdicionar.setEnabled(true);
            btCancelar.setEnabled(false);
            btEditar.setEnabled(false);
            btRemover.setEnabled(false);
            btVoltar.setEnabled(true);

        } catch (Exception e) {
        }
    }//GEN-LAST:event_btRemoverActionPerformed

    private void btCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCancelarActionPerformed
        btAdicionar.setEnabled(true);
        btCancelar.setEnabled(false);
        btEditar.setEnabled(false);
        btRemover.setEnabled(false);
        btVoltar.setEnabled(true);
    }//GEN-LAST:event_btCancelarActionPerformed

    private void txPesquisaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txPesquisaMouseClicked
        if (pesquisa == false) {
            txPesquisa.setText("");
            txPesquisa.setForeground(Color.BLACK);
            pesquisa = true;
        }
    }//GEN-LAST:event_txPesquisaMouseClicked

    private void txPesquisaFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txPesquisaFocusLost
        if (txPesquisa.getText() == null ? "" == null : txPesquisa.getText().equals("")) {
            txPesquisa.setForeground(new Color(153, 153, 153));
            txPesquisa.setText("Digite um nome para ser pesquisado");
            pesquisa = false;
        }
    }//GEN-LAST:event_txPesquisaFocusLost

    private void txPesquisaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txPesquisaKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            btPesquisaActionPerformed(null);
        }
    }//GEN-LAST:event_txPesquisaKeyPressed

    private void btAdicionarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAdicionarActionPerformed
        DialogCadServico cadServico = new DialogCadServico(null, true, this.idEmpresa);
        cadServico.DialogCadServico("Salvar");
        cadServico.show();
        configurarTabela(null);
    }//GEN-LAST:event_btAdicionarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(DialogServico.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(DialogServico.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(DialogServico.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(DialogServico.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                DialogServico dialog = new DialogServico(new javax.swing.JFrame(), true, 0);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btAdicionar;
    private javax.swing.JButton btCancelar;
    private javax.swing.JButton btEditar;
    private javax.swing.JButton btPesquisa;
    private javax.swing.JButton btRemover;
    private javax.swing.JButton btVoltar;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTable tabelaServico;
    private javax.swing.JTextField txPesquisa;
    // End of variables declaration//GEN-END:variables
}
